package com.itheima.dubbo.service;

import com.itheima.dubbo.User;
import com.itheima.dubbo.UserInterface;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Value;

//@DubboService
//@DubboService(timeout = 1000)	// 提供方指定配置超时时间 -- 接口级别
//@DubboService(methods = {   // 提供方指定配置超时时间 -- 方法级别
//        @Method(name = "getById", timeout = 1800)
//})
//@DubboService(retries = 0)  // 重复次数指定：0
// 幂等：http请求时，不管调用几次，结果都一样      GET(查询)、DELETE(删除)、PUT(表单修改)
// 非幂等：http请求时，不管调用几次，结果都变化    POST(新增)、SET(修改操作)
//@DubboService(version = "1.0.1")   // 灰度发布 - 多版本
@DubboService
public class UserServiceImpl implements UserInterface {

    @Override
    public String hello(String username) {
        username = "张三";
        System.out.println("UserServiceImpl......");
        return "Hello Dobbo username: " + username;
    }

    @Value("${server.port}")
    private Integer port;

    @Override
    public User getById(User user) {

        User userResult = new User(user.getId(), "上海黑马程序员", 7);

//        System.out.println("UserServiceImpl getById is run...1.0.1");
        System.out.println("UserServiceImpl getById is run..." + port);

        // 获取隐式参数
        String itname = RpcContext.getContext().getAttachment("itname");
        System.out.println(itname);

//        // ...超时模拟
//        try {
//            Thread.sleep(4000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        return userResult;
    }
}
